﻿using System;
using System.Globalization;
using System.Web.Security.AntiXss;
using System.Web.UI.WebControls;
using InhollandAir.auxiliary;

namespace InhollandAir
{
    public partial class Corporate : System.Web.UI.Page
    {
        private static Customer _customer;
        private static Airplane _airplane;
        private static Airport _fromAirport;
        private static Airport _toAirport;
        private static DateTime _startDate;

        protected void Page_Load(object sender, EventArgs e)
        {
            CustomerFactory.TryLogin((customer, success) => {
                if (!success) Response.Redirect("Login.aspx");
                _customer = customer;
            });

            ChooseFlight.DidChangeAirportFrom += o => CheckPlanes();
            ChooseFlight.DidChangeAirportTo += o => CheckPlanes();
        }

        private void CheckPlanes()
        {
            if (ChooseFlight.AirportFrom != null && ChooseFlight.AirportTo != null)
            {
                var list = Sql.GetAirplanes();
                ddlAirplane.Items.Clear();
                ddlAirplane.Items.Add(new ListItem("Select airplane ...", "-1"));
                foreach (var item in list)
                {
                    ddlAirplane.Items.Add(new ListItem(item.Name + " - " + item.Capacity  + " persons", item.Id.ToString(CultureInfo.InvariantCulture)));
                }

                _fromAirport = ChooseFlight.AirportFrom;
                _toAirport = ChooseFlight.AirportTo;

                lblFrom.Text = AntiXssEncoder.HtmlEncode(ChooseFlight.CountryFrom.Name + " - " + ChooseFlight.AirportFrom.Name, false);
                lblTo.Text = AntiXssEncoder.HtmlEncode(ChooseFlight.CountryTo.Name + " - " + ChooseFlight.AirportTo.Name, false);

                DatePanel.Visible = true;
            }
            else
            {
                DatePanel.Visible = false;
            }
        }

        protected void DdlAirplaneChanged(object sender, EventArgs e)
        {
            var airplane = Sql.GetAirplaneWithId(int.Parse(ddlAirplane.SelectedValue));
            _airplane = airplane;
            if (airplane == null)
            {
                BookPanel.Visible = false;
            }
            else
            {
                lblPrice.Text = AntiXssEncoder.HtmlEncode("€" + string.Format("{0:N2}", airplane.Price), false);
                lblAirplane.Text = AntiXssEncoder.HtmlEncode(airplane.Name, false);
                lblCapacity.Text = AntiXssEncoder.HtmlEncode(airplane.Capacity.ToString(CultureInfo.InvariantCulture) + " persons", false);
                BookPanel.Visible = true;
            }
        }

        protected void Logout(object sender, EventArgs e)
        {
            CustomerFactory.Logout(true);
        }

        protected void Book(object sender, EventArgs e)
        {
            if(!Page.IsValid) return;

            bool booked = CustomerFactory.Book(_customer, _airplane, _fromAirport, _toAirport, _startDate);
            if (booked)
            {
                lblDAirplane.Text = AntiXssEncoder.HtmlEncode(_airplane.Name, false);
                lblDCapacity.Text = AntiXssEncoder.HtmlEncode(_airplane.Capacity + " persons", false);
                lblDDate.Text = AntiXssEncoder.HtmlEncode(_startDate.ToString("dd-MM-yyyy HH:mm"), false);
                lblDFrom.Text = AntiXssEncoder.HtmlEncode(ChooseFlight.CountryTo.Name + " - " + _fromAirport.Name, false);
                lblDto.Text = AntiXssEncoder.HtmlEncode(ChooseFlight.CountryFrom.Name + " - " + _toAirport.Name, false);
                lblDPrice.Text = AntiXssEncoder.HtmlEncode("€" + string.Format("{0:N2}", _airplane.Price), false);
                
                MainPanel.Visible = false;
                DonePanel.Visible = true;
            }
            else
            {
                MainPanel.Visible = true;
                DonePanel.Visible = false;
            }
        }

        protected void startDate_TextChanged(object sender, EventArgs e)
        {
            DateTime dateTime;
            if (!DateTime.TryParse(startDate.Text, out dateTime))
            {
                ddlAirplane.SelectedIndex = 0;
                BookPanel.Visible = false;
                PlanePanel.Visible = false;

            }

            _startDate = dateTime;
            lblStartDate.Text = AntiXssEncoder.HtmlEncode(_startDate.ToString("dd-MM-yyyy HH:mm"), false);
            PlanePanel.Visible = true;
        }
    }
}